package com.xk.meeting.dao;

import com.xk.meeting.entity.Meeting;
import com.xk.meeting.utils.ConnectionUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class MeetingDaoImpl implements MeetingDao{
    private JdbcTemplate template = new JdbcTemplate(ConnectionUtils.getDataSource());
    @Override
    public int addMeeting(Meeting meeting) {
        String sql = "insert into meeting values(null,?,?,?,?,?)";
        return template.update(sql,meeting.getName(),meeting.getNum(),
                meeting.getStatus(),meeting.getAddress(),meeting.getRemarks());
    }

    @Override
    public List<Meeting> getMeetingList() {
        String sql = "select * from meeting where status = '可使用'";
        return template.query(sql,new BeanPropertyRowMapper<Meeting>(Meeting.class));
    }

    @Override
    public int deleteMeeting(int id) {
        String sql = "delete from meeting where id = ?";
        return template.update(sql,id);
    }

    @Override
    public Meeting getMeetingById(int id) {
        String sql = "select * from meeting where id =?";
        return template.queryForObject(sql,new BeanPropertyRowMapper<Meeting>(Meeting.class),id);
    }

    @Override
    public int updateMeeting(Meeting meeting) {
        String sql = "update meeting set name = ?,num = ?,status = ?,address = ?,remarks = ? where id = ?";
        return template.update(sql,meeting.getName(),meeting.getNum(),meeting.getStatus(),
                meeting.getAddress(),meeting.getRemarks(),meeting.getId());
    }

    @Override
    public String getNameById(int mid) {
        String sql = "select name from meeting where id = ?";
        return template.queryForObject(sql,String.class,mid);
    }

    @Override
    public List<Meeting> getMeetingLists() {
        String sql = "select * from meeting";
        return template.query(sql, new BeanPropertyRowMapper<Meeting>(Meeting.class));
    }
}
